{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Fine-tune with Pretrained Models\n",
    "\n",
    "Many of the exciting deep learning algorithms for computer vision require\n",
    "massive datasets for training. The most popular benchmark dataset,\n",
    "[ImageNet](http://www.image-net.org/), for example, contains one million images\n",
    "from one thousand categories. But for any practical problem, we typically have\n",
    "access to comparatively small datasets. In these cases, if we were to train a\n",
    "neural network's weights from scratch, starting from random initialized\n",
    "parameters, we would overfit the training set badly.\n",
    "\n",
    "One approach to get around this problem is to first pretrain a deep net on a\n",
    "large-scale dataset, like ImageNet. Then, given a new dataset, we can start\n",
    "with these pretrained weights when training on our new task. This process is\n",
    "commonly called _fine-tuning_. There are a number of variations of fine-tuning.\n",
    "Sometimes, the initial neural network is used only as a _feature extractor_.\n",
    "That means that we freeze every layer prior to the output layer and simply learn\n",
    "a new output layer. In [another document](https://github.com/dmlc/mxnet-notebooks/blob/master/python/how_to/predict.ipynb), we explained how to\n",
    "do this kind of feature extraction. Another approach is to update all of\n",
    "the network's weights for the new task, and that's the approach we demonstrate in\n",
    "this document.\n",
    "\n",
    "To fine-tune a network, we must first replace the last fully-connected layer\n",
    "with a new one that outputs the desired number of classes. We initialize its\n",
    "weights randomly. Then we continue training as normal. Sometimes it's common to\n",
    "use a smaller learning rate based on the intuition that we may already be close\n",
    "to a good result.\n",
    "\n",
    "In this demonstration, we'll fine-tune a model pretrained on ImageNet to the\n",
    "smaller caltech-256 dataset. Following this example, you can fine-tune to other\n",
    "datasets, even for strikingly different applications such as face\n",
    "identification.\n",
    "\n",
    "We will show that, even with simple hyper-parameters setting, we can match and\n",
    "even outperform state-of-the-art results on caltech-256.\n",
    "\n",
    "```eval_rst\n",
    ".. list-table::\n",
    "   :header-rows: 1\n",
    "\n",
    "   * - Network \n",
    "     - Accuracy \n",
    "   * - Resnet-50 \n",
    "     - 77.4% \n",
    "   * - Resnet-152 \n",
    "     - 86.4% \n",
    "```\n",
    "\n",
    "## Prepare data\n",
    "\n",
    "We follow the standard protocol to sample 60 images from each class as the\n",
    "training set, and the rest for the validation set. We resize images into 256x256\n",
    "size and pack them into the rec file. The scripts to prepare the data is as\n",
    "following.\n",
    "\n",
    "> In order to successfully run the following bash script on Windows please use https://cygwin.com/install.html .\n",
    "\n",
    "```sh\n",
    "wget http://www.vision.caltech.edu/Image_Datasets/Caltech256/256_ObjectCategories.tar\n",
    "tar -xf 256_ObjectCategories.tar\n",
    "\n",
    "mkdir -p caltech_256_train_60\n",
    "for i in 256_ObjectCategories/*; do\n",
    "    c=`basename $i`\n",
    "    mkdir -p caltech_256_train_60/$c\n",
    "    for j in `ls $i/*.jpg | shuf | head -n 60`; do\n",
    "        mv $j caltech_256_train_60/$c/\n",
    "    done\n",
    "done\n",
    "\n",
    "python ~/mxnet/tools/im2rec.py --list --recursive caltech-256-60-train caltech_256_train_60/\n",
    "python ~/mxnet/tools/im2rec.py --list --recursive caltech-256-60-val 256_ObjectCategories/\n",
    "python ~/mxnet/tools/im2rec.py --resize 256 --quality 90 --num-thread 16 caltech-256-60-val 256_ObjectCategories/\n",
    "python ~/mxnet/tools/im2rec.py --resize 256 --quality 90 --num-thread 16 caltech-256-60-train caltech_256_train_60/\n",
    "```\n",
    "\n",
    "The following code downloads the pregenerated rec files. It may take a few minutes."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os, sys\n",
    "\n",
    "if sys.version_info[0] >= 3:\n",
    "    from urllib.request import urlretrieve\n",
    "else:\n",
    "    from urllib import urlretrieve\n",
    "\n",
    "def download(url):\n",
    "    filename = url.split(\"/\")[-1]\n",
    "    if not os.path.exists(filename):\n",
    "        urlretrieve(url, filename)\n",
    "download('http://data.mxnet.io/data/caltech-256/caltech-256-60-train.rec')\n",
    "download('http://data.mxnet.io/data/caltech-256/caltech-256-60-val.rec')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Next, we define the function which returns the data iterators:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import mxnet as mx\n",
    "\n",
    "def get_iterators(batch_size, data_shape=(3, 224, 224)):\n",
    "    train = mx.io.ImageRecordIter(\n",
    "        path_imgrec         = './caltech-256-60-train.rec',\n",
    "        data_name           = 'data',\n",
    "        label_name          = 'softmax_label',\n",
    "        batch_size          = batch_size,\n",
    "        data_shape          = data_shape,\n",
    "        shuffle             = True,\n",
    "        rand_crop           = True,\n",
    "        rand_mirror         = True)\n",
    "    val = mx.io.ImageRecordIter(\n",
    "        path_imgrec         = './caltech-256-60-val.rec',\n",
    "        data_name           = 'data',\n",
    "        label_name          = 'softmax_label',\n",
    "        batch_size          = batch_size,\n",
    "        data_shape          = data_shape,\n",
    "        rand_crop           = False,\n",
    "        rand_mirror         = False)\n",
    "    return (train, val)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We then download a pretrained 50-layer ResNet model and load it into memory. Note\n",
    "that if `load_checkpoint` reports an error, we can remove the downloaded files\n",
    "and try `get_model` again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_model(prefix, epoch):\n",
    "    download(prefix+'-symbol.json')\n",
    "    download(prefix+'-%04d.params' % (epoch,))\n",
    "\n",
    "get_model('http://data.mxnet.io/models/imagenet/resnet/50-layers/resnet-50', 0)\n",
    "sym, arg_params, aux_params = mx.model.load_checkpoint('resnet-50', 0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Train\n",
    "\n",
    "We first define a function which replaces the last fully-connected layer for a given network."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_fine_tune_model(symbol, arg_params, num_classes, layer_name='flatten0'):\n",
    "    \"\"\"\n",
    "    symbol: the pretrained network symbol\n",
    "    arg_params: the argument parameters of the pretrained model\n",
    "    num_classes: the number of classes for the fine-tune datasets\n",
    "    layer_name: the layer name before the last fully-connected layer\n",
    "    \"\"\"\n",
    "    all_layers = symbol.get_internals()\n",
    "    net = all_layers[layer_name+'_output']\n",
    "    net = mx.symbol.FullyConnected(data=net, num_hidden=num_classes, name='fc1')\n",
    "    net = mx.symbol.SoftmaxOutput(data=net, name='softmax')\n",
    "    new_args = dict({k:arg_params[k] for k in arg_params if 'fc1' not in k})\n",
    "    return (net, new_args)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we create a module. Note we pass the existing parameters from the loaded model via the `arg_params` argument.\n",
    "The parameters of the last fully-connected layer will be randomly initialized by the `initializer`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import logging\n",
    "head = '%(asctime)-15s %(message)s'\n",
    "logging.basicConfig(level=logging.DEBUG, format=head)\n",
    "\n",
    "def fit(symbol, arg_params, aux_params, train, val, batch_size, num_gpus):\n",
    "    devs = [mx.gpu(i) for i in range(num_gpus)]\n",
    "    mod = mx.mod.Module(symbol=symbol, context=devs)\n",
    "    mod.fit(train, val,\n",
    "        num_epoch=8,\n",
    "        arg_params=arg_params,\n",
    "        aux_params=aux_params,\n",
    "        allow_missing=True,\n",
    "        batch_end_callback = mx.callback.Speedometer(batch_size, 10),\n",
    "        kvstore='device',\n",
    "        optimizer='sgd',\n",
    "        optimizer_params={'learning_rate':0.01},\n",
    "        initializer=mx.init.Xavier(rnd_type='gaussian', factor_type=\"in\", magnitude=2),\n",
    "        eval_metric='acc')\n",
    "    metric = mx.metric.Accuracy()\n",
    "    return mod.score(val, metric)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Then we can start training. We use AWS EC2 g2.8xlarge, which has 8 GPUs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "num_classes = 256\n",
    "batch_per_gpu = 16\n",
    "num_gpus = 8\n",
    "\n",
    "(new_sym, new_args) = get_fine_tune_model(sym, arg_params, num_classes)\n",
    "\n",
    "batch_size = batch_per_gpu * num_gpus\n",
    "(train, val) = get_iterators(batch_size)\n",
    "mod_score = fit(new_sym, new_args, aux_params, train, val, batch_size, num_gpus)\n",
    "assert mod_score > 0.77, \"Low training accuracy.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You will see that, after only 8 epochs, we can get 78% validation accuracy. This\n",
    "matches the state-of-the-art results training on caltech-256 alone,\n",
    "e.g. [VGG](http://www.robots.ox.ac.uk/~vgg/research/deep_eval/).\n",
    "\n",
    "Next, we try to use another pretrained model. This model was trained on the\n",
    "complete Imagenet dataset, which is 10x larger than the Imagenet 1K classes\n",
    "version, and uses a 3x deeper Resnet architecture."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "get_model('http://data.mxnet.io/models/imagenet-11k/resnet-152/resnet-152', 0)\n",
    "sym, arg_params, aux_params = mx.model.load_checkpoint('resnet-152', 0)\n",
    "(new_sym, new_args) = get_fine_tune_model(sym, arg_params, num_classes)\n",
    "mod_score = fit(new_sym, new_args, aux_params, train, val, batch_size, num_gpus)\n",
    "assert mod_score > 0.86, \"Low training accuracy.\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "As can be seen, even for a single data epoch, it reaches 83% validation\n",
    "accuracy. After 8 epoches, the validation accuracy increases to 86.4%.\n",
    "\n",
    "<!-- INSERT SOURCE DOWNLOAD BUTTONS -->\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "display_name": "",
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.12"
  },
  "name": ""
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
